<template>
  <div>
    <el-card shadow="never" class="d2-mb">
      <el-form
        class="filter-container"
        ref="XHForm"
        :model="XHForm"
        label-width="85px"
      >
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="品类">
              <el-input placeholder="请输入内容" v-model="XHForm.category" clearable>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="品名">
              <el-input placeholder="请输入内容" v-model="XHForm.name" clearable/>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="品牌">
              <el-input placeholder="请输入内容" v-model="XHForm.commodity_spot__brand" clearable>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态">
               <el-select  clearable v-model="XHForm.status" placeholder="请选择" filterable allow-create>
			            <el-option label="停用" value="0"></el-option>
                  <el-option label="启用" value="1"></el-option>
			        </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="2">
          <el-button type="primary" size="small" @click="handle_PMIDChange" style='margin-top:3px;'>
            <d2-icon name="search" /> 查询
          </el-button>
        </el-col>
        </el-row>
      </el-form>
    </el-card>
    <div class="mb">
      <el-button type="default" size="small" @click="addPMList">
        <d2-icon name="plus" />
        新增
      </el-button>
      <el-button type="default" size="small" @click="editPMList">
        <d2-icon name="edit" />
        修改
      </el-button>
      <el-button type="default" size="small" icon="el-icon-delete" @click="delPMList">
        删除
      </el-button>
      <!-- <el-button type="default" size="small" @click="examine">
        <d2-icon name="search" />
        查看
      </el-button> -->
      <el-upload
            class="upload-btn"
            :action="uploadUrl"
            accept=".xls, .xlsx"
            :on-success="handleAvatarSuccess"
            :on-progress="uploadProgress"
            :on-error="handleUploadError"
            :show-file-list="false" style="display:inline-block;margin:0 10px;">
          <el-button type="default" size="small">
            <d2-icon name="download" />
            导入
          </el-button>
          </el-upload>
      <!-- <el-button type="default" size="small" @click="handleFormSubmit">
        <d2-icon name="upload" />
        导出
      </el-button> -->
      <el-button type="default" size="small" @click="StatusPMList">
        <d2-icon name="unlock" />
        启用
      </el-button>
      <el-button type="default" size="small" @click="StatusPMList">
        <d2-icon name="unlock-alt" />
        停用
      </el-button>
      <el-button
        type="default"
        size="small"
        @click="handlerefreshCurrent"
      >
        <d2-icon name="refresh" />
        刷新
      </el-button>
    </div>
    <el-card shadow="never" class="d2-mb">
      <template slot="header" style="height:66px;">
        <span class="title_x el-button--primary fl"></span>
        <p class="title_l fl">现货信息</p>
      </template>
        <el-table :data="PMNameList" border :header-cell-style="{ background: '#eef1f6' }" ref="multipleTable" highlight-current-row @selection-change="handleSelectionChange">
           <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column type="index" label="序号" width="75" sortable></el-table-column>
          <el-table-column prop="status" label="状态" :formatter="PMStatus"></el-table-column>
          <el-table-column prop="category" label="品类"></el-table-column>
          <el-table-column prop="name" label="品名"></el-table-column>
          <el-table-column prop="id" label="品名ID"></el-table-column>
          <el-table-column prop="commodity_unit" label="商品单位" :formatter="SPFormat"></el-table-column>
          <el-table-column prop="measure_unit" label="计量单位" :formatter="JLFormat"></el-table-column>
          <el-table-column prop="tax_typeName" label="税种"></el-table-column>
          <el-table-column prop="taxType_start_date" label="开始日期" sortable width="105">
            <template slot-scope="scope">{{scope.row.taxType_start_date | dateYMDHMSFormat}}</template>
          </el-table-column>
           <el-table-column prop="taxType_rate" label="税率"></el-table-column>
          <el-table-column prop="brand" label="品牌"></el-table-column>
          <el-table-column prop="origin" label="产地"></el-table-column>
          <el-table-column prop="standard" label="规格"></el-table-column>
          <el-table-column prop="attribute" label="主属性"></el-table-column>
          <el-table-column prop="desc" label="备注"></el-table-column>
        </el-table>
    </el-card>
    <!---------------------------------------------- 新增 ------------------------------------------------>
    <el-dialog :visible.sync="add_PMVisible" title="现货信息-新增" width="83%">
      <div class="mb">
      <el-button type="default" size="small" @click="addRow">
        <d2-icon name="plus" />
        增行
      </el-button>
      <el-button type="default" size="small" icon="el-icon-delete" @click="delData">
        删行
      </el-button>
    </div>
     <el-form label-position="right" status-icon :rules="rules" :model="pmtableData" ref="ruleForm">
    <el-table :data="pmtableData" border :cell-style="{ 'text-align': 'center' }" :header-cell-style="{ 'text-align': 'center' }" @selection-change='selectRow'>
      <el-table-column type="selection" width="40" align="center"></el-table-column>
      <el-table-column type="index" label="序号" width="55">
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <el-form-item :prop="scope.$index + '.status'" :rules="rules.status">
           <el-select  clearable v-model="scope.row.status" placeholder="请选择" filterable allow-create>
			         <el-option label="停用" value="0"></el-option>
               <el-option label="启用" value="1"></el-option>
			     </el-select>
          </el-form-item>
         </template>
      </el-table-column>
      <el-table-column label="品类" width="150">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.category'" :rules="rules.category">
                  <el-input class="remark" v-model="scope.row.category"></el-input>
           </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="品名" width="150">
       <template slot-scope="scope">
          <el-form-item :prop="scope.$index + '.name'" :rules="rules.name">
                  <el-input class="remark" v-model="scope.row.name"></el-input>
          </el-form-item>
                </template>
      </el-table-column>
      <!-- <el-table-column prop="id" label="品名ID" sortable>
        <template slot-scope="scope">
                  <el-input class="remark" v-model="scope.row.id"></el-input>
                </template>
      </el-table-column> -->
      <el-table-column label="商品单位" width="110">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.commodity_unit'" :rules="rules.commodity_unit">
           <el-select  clearable v-model="scope.row.commodity_unit" placeholder="请选择" filterable allow-create>
			         <el-option label="吨" value="0"></el-option>
               <el-option label="千克" value="1"></el-option>
                <el-option label="桶" value="2"></el-option>
               <el-option label="立方米" value="3"></el-option>
                <el-option label="张" value="4"></el-option>
               <el-option label="克" value="5"></el-option>
			     </el-select>
           </el-form-item>
         </template></el-table-column>
      <el-table-column label="计量单位" width="110">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.measure_unit'" :rules="rules.measure_unit">
           <el-select  clearable v-model="scope.row.measure_unit" placeholder="请选择" filterable allow-create>
			       <el-option label="吨" value="0"></el-option>
               <el-option label="千克" value="1"></el-option>
                <el-option label="桶" value="2"></el-option>
               <el-option label="立方米" value="3"></el-option>
                <el-option label="张" value="4"></el-option>
               <el-option label="克" value="5"></el-option>
			     </el-select>
           </el-form-item>
         </template>
      </el-table-column>
      <el-table-column label="税种" width="150">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.taxType_name'" :rules="rules.taxType_name">
                  <el-input class="remark" v-model="scope.row.taxType_name"></el-input>
           </el-form-item>
                </template>
      </el-table-column>
      <el-table-column prop="taxType_start_date" label="开始日期" sortable width="160">
       <template slot-scope="scope">
          <el-form-item :prop="scope.$index + '.taxType_start_date'" :rules="rules.taxType_start_date">
                  <el-date-picker v-model="scope.row.taxType_start_date" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width:100%;padding-right:0;"></el-date-picker>
          </el-form-item>
                </template>
                </el-table-column>
      <el-table-column label="税率" width="100">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.taxType_rate'" :rules="rules.taxType_rate">
                  <el-input class="remark" v-model="scope.row.taxType_rate" type="number" :max="1" :step="0.1" :min="0"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="品牌" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.brand'" :rules="rules.brand">
                  <el-input class="remark" v-model="scope.row.brand"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="产地" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.origin'" :rules="rules.origin">
                  <el-input class="remark" v-model="scope.row.origin"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="规格" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.standard'" :rules="rules.standard">
                  <el-input class="remark" v-model="scope.row.standard"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="主属性" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.attribute'" :rules="rules.attribute">
                  <el-input class="remark" v-model="scope.row.attribute"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.desc'" :rules="rules.desc">
                  <el-input class="remark" v-model="scope.row.desc"></el-input>
           </el-form-item>
                </template>
      </el-table-column>
    </el-table>
     </el-form>
      <span span slot="footer" class="dialog-footer">
        <el-button @click="add_PMVisible = false">取消</el-button>
        <el-button type="primary" @click="addPMListB('ruleForm')">保存</el-button
        >
      </span>
    </el-dialog>
     <!---------------------------------------------- 修改 ------------------------------------------------>
    <el-dialog :visible.sync="edit_PMVisible" title="现货信息-修改" width="83%">
     <el-form label-position="right" status-icon :rules="rules" :model="edit_pmtableData" ref="ruleForm">
    <el-table :data="edit_pmtableData" border>
      <el-table-column type="index" label="序号" width="55">
      </el-table-column>
     <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <el-form-item :prop="scope.$index + '.status'" :rules="rules.status">
           <el-select  clearable v-model="scope.row.status" placeholder="请选择" filterable allow-create>
			         <el-option label="停用" value="0"></el-option>
               <el-option label="启用" value="1"></el-option>
			     </el-select>
          </el-form-item>
         </template>
      </el-table-column>
      <el-table-column label="品类" width="150">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.category'" :rules="rules.category">
                  <el-input class="remark" v-model="scope.row.category"></el-input>
           </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="品名" width="150">
       <template slot-scope="scope">
          <el-form-item :prop="scope.$index + '.name'" :rules="rules.name">
                  <el-input class="remark" v-model="scope.row.name"></el-input>
          </el-form-item>
                </template>
      </el-table-column>
      <el-table-column prop="id" label="品名ID">
        <template slot-scope="scope">
                  <el-input class="remark" v-model="scope.row.id" disabled></el-input>
                </template>
      </el-table-column>
       <el-table-column label="商品单位" width="110">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.commodity_unit'" :rules="rules.commodity_unit">
           <el-select  clearable v-model="scope.row.commodity_unit" placeholder="请选择" filterable allow-create>
			         <el-option label="吨" value="0"></el-option>
               <el-option label="千克" value="1"></el-option>
                <el-option label="桶" value="2"></el-option>
               <el-option label="立方米" value="3"></el-option>
                <el-option label="张" value="4"></el-option>
               <el-option label="克" value="5"></el-option>
			     </el-select>
           </el-form-item>
         </template></el-table-column>
      <el-table-column label="计量单位" width="110">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.measure_unit'" :rules="rules.measure_unit">
           <el-select  clearable v-model="scope.row.measure_unit" placeholder="请选择" filterable allow-create>
			        <el-option label="吨" value="0"></el-option>
               <el-option label="千克" value="1"></el-option>
                <el-option label="桶" value="2"></el-option>
               <el-option label="立方米" value="3"></el-option>
                <el-option label="张" value="4"></el-option>
               <el-option label="克" value="5"></el-option>
			     </el-select>
           </el-form-item>
         </template>
      </el-table-column>
      <el-table-column label="税种" width="150">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.taxType_name'" :rules="rules.taxType_name">
                  <el-input class="remark" v-model="scope.row.taxType_name"></el-input>
           </el-form-item>
                </template>
      </el-table-column>
      <el-table-column prop="taxType_start_date" label="开始日期" sortable width="160">
       <template slot-scope="scope">
          <el-form-item :prop="scope.$index + '.taxType_start_date'" :rules="rules.taxType_start_date">
                  <el-date-picker v-model="scope.row.taxType_start_date" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width:100%;padding-right:0;"></el-date-picker>
          </el-form-item>
                </template>
                </el-table-column>
      <el-table-column label="税率" width="100">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.taxType_rate'" :rules="rules.taxType_rate">
                  <el-input class="remark" v-model="scope.row.taxType_rate"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="品牌" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.brand'" :rules="rules.brand">
                  <el-input class="remark" v-model="scope.row.brand"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="产地" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.origin'" :rules="rules.origin">
                  <el-input class="remark" v-model="scope.row.origin"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="规格" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.standard'" :rules="rules.standard">
                  <el-input class="remark" v-model="scope.row.standard"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="主属性" width="150">
         <template slot-scope="scope">
            <el-form-item :prop="scope.$index + '.attribute'" :rules="rules.attribute">
                  <el-input class="remark" v-model="scope.row.attribute"></el-input>
            </el-form-item>
                </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
           <el-form-item :prop="scope.$index + '.desc'" :rules="rules.desc">
                  <el-input class="remark" v-model="scope.row.desc"></el-input>
           </el-form-item>
                </template>
      </el-table-column>
    </el-table>
     </el-form>
      <span span slot="footer" class="dialog-footer">
        <el-button @click="edit_PMVisible = false">取消</el-button>
        <el-button type="primary" @click="editPMListB('ruleForm')">保存</el-button
        >
      </span>
    </el-dialog>
    <!-- 导入 -->
    <el-dialog :visible.sync="drPMVisible" title="导入现货信息" width="83%">
    <el-table :data="drpmtableData" border :cell-style="{ 'text-align': 'center' }" :header-cell-style="{ 'text-align': 'center' }" @selection-change='selectRow'>
      <el-table-column type="index" label="序号">
      </el-table-column>
      <el-table-column label="状态" prop="status">
      </el-table-column>
      <el-table-column label="品类" prop="category">
      </el-table-column>
      <el-table-column label="品名ID" prop="name">
      </el-table-column>
      <el-table-column label="商品单位" prop="commodity_unit">
      </el-table-column>
      <el-table-column label="计量单位" prop="measure_unit">
      </el-table-column>
      <el-table-column label="税种" prop="taxType_name">
      </el-table-column>
      <el-table-column label="开始日期" prop="taxType_start_date">
      </el-table-column>
      <el-table-column label="税率" prop="taxType_rate">
      </el-table-column>
      <el-table-column label="品牌" prop="brand">
      </el-table-column>
      <el-table-column label="产地" prop="origin">
      </el-table-column>
      <el-table-column label="规格" prop="standard">
      </el-table-column>
      <el-table-column label="主属性" prop="attribute">
      </el-table-column>
      <el-table-column label="备注" prop="desc">
      </el-table-column>
    </el-table>
      <span span slot="footer" class="dialog-footer">
        <el-button @click="drPMVisible = false">取消</el-button>
        <el-button type="primary" @click="drpm">保存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import DemoPageFooter from "../../../../../components/PageFooter";
// import spotInformation_M from "../../componnets/merchandise_news/model/spotInformation_M.vue";
import { conCommodity, addCommodity, addcustomer } from '../../../../../api/url_lcc';
export default {
  //商品信息-现货信息
  name: "spotInformation",
  components: {
    "demo-page-footer": DemoPageFooter,
    // "v-spotInformation": spotInformation_M
  },
  data() {
    return {
       // 导入
      uploadUrl:`/user/lead/`,
      drPMVisible:false,
      drpmtableData:[],
      add_PMVisible: false,
      edit_PMVisible:false,
      pmtableData:[{
        brand:"",
        origin:"",
        standard:"",
        attribute:"",
        desc:"",
      }],
       row: "",
      rowNum: 1,
      XHForm: {},
      page: {
        pageCurrent: 1,
        pageSize: 20,
        pageTotal: 0
      },
      PMNameList:[],
      ruleForm:{
            taxType_name:"",
            taxType_rate: "",
            taxType_start_date: "",
            brand: "",
            origin: "",
            standard: "",
            attribute: "",
            name: "",
            category: "",
            commodity_unit: "",
            measure_unit: "",
            status: "",
            desc: ""
      },
      rules:{
        status: [
            { required: true, message: '请选择状态', trigger: 'change' }
          ],
        category: [
            { required: true, message: '请输入品类', trigger: 'blur' },
          ],
        name: [
         { required: true, message: '请输入品名', trigger: 'blur' },
       ],
        id: [
         { required: true, message: '请输入品名ID', trigger: 'blur' },
       ],
       commodity_unit: [
            { required: true, message: '请选择商品单位', trigger: 'change' }
          ],
       measure_unit: [
            { required: true, message: '请选择计量单位', trigger: 'change' }
          ],
       tax_typeName: [
         { required: true, message: '请输入税种', trigger: 'blur' },
       ],
       taxType_name: [
         { required: true, message: '请输入税种', trigger: 'blur' },
       ],
        taxType_start_date: [
            { type: 'string', required: true, message: '请选择开始日期', trigger: 'change' }
          ],
       taxType_rate: [
         { required: true, message: '请输入税率', trigger: 'blur' },
       ], 
      //  brand: [
      //    { required: true, message: '请输入品牌', trigger: 'blur' },
      //  ], 
      //  origin: [
      //    { required: true, message: '请输入产地', trigger: 'blur' },
      //  ], 
      //  standard: [
      //    { required: true, message: '请输入规格', trigger: 'blur' },
      //  ], 
      //  attribute: [
      //    { required: true, message: '请输入主属性', trigger: 'blur' },
      //  ], 
      //  desc: [
      //    { required: true, message: '请输入备注', trigger: 'blur' },
      //  ], 
      },
    };
  },
   created() {
           this.tabledatas.map(i => {
               i.show = false
               return i
           })
  },
  methods: {
    // 导入
      handleAvatarSuccess (res, file) {
        // this.uploadloading.close();
        console.log(444444,res.result);
        if (res && res.errcode === "1") {
           this.$message({
           message: '导入成功',
           type: 'success',
          })
          this.drPMVisible = true;
          this.drpmtableData = [];
          var data1 = res.result.Sheet1;
          for (var i = 0; i < data1.length; i++) {
            this.drpmtableData[i] = {
               status: data1[i].状态,
               category: data1[i].品类,
               name: data1[i].品名ID,
               commodity_unit: data1[i].商品单位,
               measure_unit: data1[i].计量单位,
               taxType_name: data1[i].税种,
               taxType_start_date: data1[i].开始日期,
               taxType_rate: data1[i].税率,
               brand: data1[i].品牌,
               origin: data1[i].产地,
               standard: data1[i].规格,
               attribute: data1[i].主属性,
               desc: data1[i].备注
            }
          }
         
        } else {
          this.$message.error(res.msg);
        }
      },
      drpm(){
         var drpmtableData = [];
        var drpmtableData = this.drpmtableData;
        drpmtableData.forEach(function(item, index) {
           var data1 = item.status
           var data2 = item.commodity_unit
           var data3 = item.measure_unit
          if (data1 == "停用") {
             item.status = '0';
         } else if (data1 == "启用") {
             item.status = '1';
         }
          if (data2 == "吨") {
             item.commodity_unit = '0';
         } else if (data2 == "千克") {
             item.commodity_unit = '1';
         } else if (data2 == "桶") {
             item.commodity_unit = '2';
         } else if (data2 == "金属吨") {
             item.commodity_unit = '3';
         } else if (data2 == "风干吨") {
             item.commodity_unit = '4';
         } else if (data2 == "手") {
             item.commodity_unit = '5';
         }
           if (data3 == "吨") {
             item.measure_unit = '0';
         } else if (data3 == "千克") {
             item.measure_unit = '1';
         } else if (data3 == "桶") {
             item.measure_unit = '2';
         } else if (data3 == "金属吨") {
             item.measure_unit = '3';
         } else if (data3 == "风干吨") {
             item.measure_unit = '4';
         } else if (data3 == "手") {
             item.measure_unit = '5';
         }
       })
        let params = { 
          "operation":"ADD",
          commodity_data:this.drpmtableData
        };
        addCommodity(params).then(res => {
            // if (res.errcode == "1") {
                this.$message.success(res.result);
                this.drPMVisible = false;
             }).catch(err =>{
          this.$message.error(err.msg);
        });
      },
    // 品名
    PMStatus(row,column){
        return row.status == 0?'不可用':'可用'
    },
    // 单位
    SPFormat(row, column) {
         if (row.commodity_unit === "0") {
             return '吨';
         } else if (row.commodity_unit === "1") {
             return '千克';
         } else if (row.commodity_unit === "2") {
             return '桶';
         } else if (row.commodity_unit === "3") {
             return '立方米';
         } else if (row.commodity_unit === "4") {
             return '张';
         } else if (row.commodity_unit === "5") {
             return '克';
         }
     },
    JLFormat(row, column) {
         if (row.measure_unit === "0") {
             return '吨';
         } else if (row.measure_unit === "1") {
             return '千克';
         } else if (row.measure_unit === "2") {
             return '桶';
         } else if (row.measure_unit === "3") {
             return '立方米';
         } else if (row.measure_unit === "4") {
             return '张';
         } else if (row.measure_unit === "5") {
             return '克';
         }
     },
    // 品名查询
    handle_PMIDChange(){
        let params = {
            "category":this.XHForm.category||"",
            "name":this.XHForm.name||"",
            "commodity_spot__brand":this.XHForm.commodity_spot__brand||"",
            "status":this.XHForm.status||"",
        };
         conCommodity(params).then(res => {
            // if (res.errcode === "1") {
                this.PMNameList = []
                var data = res.result
                // this.PMNameList = data
                for (var i = 0; i < data.length; i++) {
                  this.PMNameList[i] = {
                      "spot_id": data[i].commodity_spot[0]["id"],
                       "brand": data[i].commodity_spot[0]["brand"],
                       "origin": data[i].commodity_spot[0]['origin'],
                       "standard": data[i].commodity_spot[0]['standard'],
                       "attribute": data[i].commodity_spot[0]['attribute'],
                       "taxType_name":data[i].tax_type.name,
                       "taxType_rate": data[i].tax_type.rate,
                       "taxType_start_date": data[i].tax_type.start_date,
                       "tax_typeName": data[i].tax_type['name'],
                       "tax_typeId": data[i].tax_type['id'],
                       "tax_typestatus": data[i].tax_type['status'],
                       "name": data[i].name,
                       'id': data[i]['id'],
                       "category": data[i]['category'],
                       "commodity_unit":data[i]['commodity_unit'].toString(),
                       "measure_unit": data[i]['measure_unit'].toString(),
                       "status": data[i].status.toString(),
                       "creator": data[i]['creator'],
                       "desc": data[i]['desc']
                  }
              }
           }).catch(err =>{
          this.$message.error(err.msg);
        });
        },
    //  新增
    addPMList(){
      this.add_PMVisible = true
    },
    // 新增保存
    addPMListB(ruleForm){
      let params = {
            operation: "ADD",
            "commodity_data":this.pmtableData
        };
        const rule1 = new Promise((resolve, reject) => {
        this.$refs[ruleForm].validate(valid => {
          if (valid) {
            resolve();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      });
      Promise.all([rule1]).then(() => {
        addCommodity(params).then(res => {
            // if (res.errcode == "1") {
                this.$message.success(res.result);
                 this.add_PMVisible = false
                 this.handle_PMIDChange()
           }).catch(err =>{
          this.$message.error(err.msg);
        });
         });
    },
    // 修改
    editPMList(){
      console.log(111111,this.multipleSelection)
      this.edit_PMVisible = true
        var data = this.multipleSelection
       if (this.multipleSelection == undefined) {
           this.openMessage();
       } else if(this.multipleSelection.length != 1){
          this.$message.error("修改只能选中一条数据进行！");
       } else {
          this.edit_pmtableData = this.multipleSelection
       }
    },
    // 修改保存
    editPMListB(ruleForm){
       let params = {
            operation: "MOD",
            "commodity_data":{
                "id": this.edit_pmtableData[0].id,
                "tax_type": {
                    "name": this.edit_pmtableData[0].taxType_name,
                    "id": this.edit_pmtableData[0].tax_typeId,
                    "rate": this.edit_pmtableData[0].taxType_rate,
                    "start_date": this.edit_pmtableData[0].taxType_start_date,
                    },
                "commodity_spot":{
                   "id": this.edit_pmtableData[0].spot_id,
                   "brand": this.edit_pmtableData[0].brand,
                   "origin": this.edit_pmtableData[0].origin,
                   "standard": this.edit_pmtableData[0].standard,
                   "attribute": this.edit_pmtableData[0].attribute
                  },
                "name": this.edit_pmtableData[0].name,
                "category": this.edit_pmtableData[0].category,
                "commodity_unit": this.edit_pmtableData[0].commodity_unit,
                "measure_unit": this.edit_pmtableData[0].measure_unit,
                "status": this.edit_pmtableData[0].status,
                "creator": this.edit_pmtableData[0].creator,
                "desc": this.edit_pmtableData[0].desc
            }
        };
         const rule1 = new Promise((resolve, reject) => {
        this.$refs[ruleForm].validate(valid => {
          if (valid) {
            resolve();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      });
      Promise.all([rule1]).then(() => {
        addCommodity(params).then(res => {
            // if (res.errcode === "1") {
                this.$message.success(res.result);
                 this.edit_PMVisible = false
                 this.handle_PMIDChange()
            }).catch(err =>{
          this.$message.error(err.msg);
        });
         });
    },
    // 删除
    delPMList(){
       if (this.multipleSelection == undefined) {
           this.openMessage();
       } else{
          this.del_message()
       }
    },
    del_message1(){
      var data = this.multipleSelection
          var Newdata  = [];
          data.forEach(function(item, index) {
           var data1 = item.id
           Newdata.push(data1)
           console.log(90,data1,91,Newdata)
        })
            let params = {
            operation: 'DEL',
            commodity_data: Newdata
        };
        addCommodity(params).then(res => {
            // if (res.errcode === "1") {
                this.$message.success(res.result);
                 this.handle_PMIDChange()
             }).catch(err =>{
          this.$message.error(err.msg);
        });
    },
    // 删除提示信息
    del_message() {
       this.$confirm('是否删除该商品信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.del_message1()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
    // 启用/停用
    StatusPMList(){
       if (this.multipleSelection == undefined) {
           this.openMessage();
       } else{
          var data = this.multipleSelection
       var Newdata  = [];
       data.forEach(function(item, index) {
           var data1 = item.id
           Newdata.push(data1)
           console.log(90,data1,91,Newdata)
        })
            let params = {
            operation: 'STATUS',
            commodity_data: Newdata
        };
        addCommodity(params).then(res => {
            // if (res.errcode === "1") {
                this.$message.success(res.result);
                 this.handle_PMIDChange()
           }).catch(err =>{
          this.$message.error(err.msg);
        });
       }
    },
    // 刷新
    handlerefreshCurrent(){
      this.XHForm.category="",
      this.XHForm.name="",
      this.XHForm.commodity_spot__brand="",
      this.XHForm.status="",
      this.handle_PMIDChange()
    },
    // 多选提示
    openMessage() {
      this.$message({
          dangerouslyUseHTMLString: true,
          type: 'error',
          message: '<strong>请勾选你要选择的行，再进行操作！</strong>'
      });
    },
    
    // 行内编辑
    handleCellDataChange({ rowIndex, key, value, row }) {
      console.log(rowIndex);
      console.log(key);
      console.log(value);
      console.log(row);
    },
    // 单选
    handleCurrentChange(currentRow, oldCurrentRow) {
      console.log(currentRow);
      console.log(oldCurrentRow);
    },
    // 多选
    handleSelectionChange(val) {
       this.multipleSelection = val;
       console.log('品名多选',this.multipleSelection)
     },
      // 获取表格选中时的数据
      selectRow (val) {
        this.selectlistRow = val
      },
      // 增加行
      addRow () {
       	  var list = {
            rowNum:this.rowNum,
            taxType_name:"",
            taxType_rate: "",
            taxType_start_date: "",
            taxType_status: "",
            brand: "",
            origin: "",
            standard: "",
            attribute: "",
            name: "",
            category: "",
            commodity_unit: "",
            measure_unit: "",
            status: "",
            desc: ""
   		 };
        this.pmtableData.unshift(list)
        this.rowNum += 1;
      },
      // 删除方法
      // 删除选中行
      delData () {
        for (let i = 0; i < this.selectlistRow.length; i++) {
          let val = this.selectlistRow
          val.forEach((val, index) => {
            this.pmtableData.forEach((v, i) => {
              if (val.rowNum === v.rowNum) {
                // i 为选中的索引
                this.pmtableData.splice(i, 1)
              }
            })
          })
        }
        // 删除完数据之后清除勾选框
        this.$refs.table.clearSelection()
      },
  
  },
  mounted() {
    this.handle_PMIDChange()

}
  
};
</script>
<style></style>
